package com.blb.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.blb.entity.Menu;
import com.blb.entity.MenuExt2;
import com.blb.entity.Role;
import com.blb.entity.RoleExample;
import com.blb.entity.Rolemenu;
import com.blb.entity.RolemenuExample;
import com.blb.mapper.MenuExtMapper;
import com.blb.mapper.RoleMapper;
import com.blb.mapper.RolemenuMapper;
import com.blb.service.IRoleService;
import com.blb.utils.LayuiPage;
import com.blb.utils.SessionUtils;
import com.blb.utils.UUIDUtils;

/**
 * 角色控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("role")
public class RoleController {
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private MenuExtMapper menuExtMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RolemenuMapper rolemenuMapper;
	
	@RequiresPermissions("role:list")
	@RequestMapping("showList")
	public String showList(String menuId, Model model) throws IOException{
		//查询当前用户拥有哪些按钮
		List<Menu> list = menuExtMapper.getButtonByUserId(SessionUtils.getUserId(), menuId);
		model.addAttribute("bts", list);
		return "role/role_list";
	}
	
	/**
	 * 显示添加界面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("showAdd")
	public String showAdd(Model model) throws IOException{
		//查询菜单树形结构数据
		List<MenuExt2 > list = menuExtMapper.getMenus("0");
		System.out.println(list);
		String data = "";
		if (!list.isEmpty()) data = JSON.toJSON(list).toString(); 
		model.addAttribute("menus", data);
		return "role/role_add";
	}

	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param role
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public LayuiPage page(int page, int limit, String role) throws IOException{
		return roleService.page((page-1)*limit, limit, role);
	}
	
	/**
	 * 判断角色是否存在
	 * @param role
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String role) throws IOException{
		return roleService.isExist(role);
	}
	
	/**
	 * 保存
	 * @param role
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Role role, String menus) throws IOException{
		role.setId(UUIDUtils.getID());
		role.setCreateTime(new Date());
		role.setCreateUser(SessionUtils.getUserName());
		roleMapper.insert(role);
		
		//保存角色菜单
		String[] menuArr = menus.split(",");
		for (String menu : menuArr) {
			Rolemenu rolemenu = new Rolemenu();
			rolemenu.setRoleId(role.getId());
			rolemenu.setMenuId(menu);
			rolemenuMapper.insert(rolemenu);
		}
		
		return "";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody List<String> ids) throws IOException{
		//删除角色
		RoleExample roleExample = new RoleExample();
		roleExample.createCriteria().andIdIn(ids);
		roleMapper.deleteByExample(roleExample);
		
		//删除角色菜单关联表
		for (String roleId : ids) {
			RolemenuExample rolemenuExample = new RolemenuExample();
			rolemenuExample.createCriteria().andRoleIdEqualTo(roleId);
			rolemenuMapper.deleteByExample(rolemenuExample);
		}
		
		return "";
	}
	
	/**
	 * 跳转修改界面
	 * @param role
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("showUpdate")
	public String showUpdate(Role role, Model model) throws IOException{
		
		//查询菜单树形结构数据
		List<MenuExt2> list = menuExtMapper.getMenus("0");
		System.out.println(list);
		String data = "";
		if (!list.isEmpty()) data = JSON.toJSON(list).toString(); 
		model.addAttribute("menus", data);
		
		//查询已经勾选的菜单
		RolemenuExample rolemenuExample = new RolemenuExample();
		rolemenuExample.createCriteria().andRoleIdEqualTo(role.getId());
		List<Rolemenu> list2 = rolemenuMapper.selectByExample(rolemenuExample);
		String choiseMenus = "";
		if (!list2.isEmpty()) choiseMenus = JSON.toJSON(list2).toString();
		
		model.addAttribute("choiseMenus", choiseMenus);
		model.addAttribute("role", role);
		return "role/role_update";
	}
	
	/**
	 * 修改
	 * @param role
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(Role role, String menus) throws IOException{
		role.setUpdateUser(SessionUtils.getUserName());
		roleMapper.updateByPrimaryKeySelective(role);
		
		//删除角色菜单信息
		RolemenuExample rolemenuExample = new RolemenuExample();
		rolemenuExample.createCriteria().andRoleIdEqualTo(role.getId());
		rolemenuMapper.deleteByExample(rolemenuExample);
		
		//保存角色菜单
		String[] menuArr = menus.split(",");
		for (String menu : menuArr) {
			Rolemenu rolemenu = new Rolemenu();
			rolemenu.setRoleId(role.getId());
			rolemenu.setMenuId(menu);
			rolemenuMapper.insert(rolemenu);
		}
		
		return "";
	}
}
